/* 清理Qt影子文件及用户文件 */
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/wait.h>

int main(int argc, char **argv)
{
    /* 变量定义 */
    DIR *handle = NULL;
    struct dirent *d = NULL;
    pid_t pid = 0;
    char str[1280] = "";

    /* 获取句柄 */
    handle = opendir("./");

    /* 扫描目录 */
    while (1)
    {
        /* 读取目录 */
        d = readdir(handle);
        if (NULL == d)
        {
            break;
        } /* end of if (NULL == d) */
    #ifdef DEBUG
        /* 扫描目录 */
        printf("%s  ", d->d_name);
    
        
        if ((d->d_type | DT_DIR) == DT_DIR)
        {
            printf("文件夹\n");
        } /* end of if (d->d_type & DT_DIR == DT_DIR) */
        else 
        {
            printf("\n");
        }
    #endif

        /* 判断是否为目录 */
        if ((d->d_type | DT_DIR) == DT_DIR  && strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, ".."))
        {
            /* 判断是否为影子文件 */
            if (strncmp(d->d_name, "build", 5) == 0)
            {
                /* 是影子文件，创建进程删除 */
                pid = fork();

                if (pid > 0)
                {
                    /* 父进程 */
                    wait(NULL);
                }
                else if (pid == 0)
                {
                    /* 子进程 */
                    execl("/usr/bin/rm", "rm",  "-rf", d->d_name, NULL);
                }
                else if (pid < 0)
                {
                    perror("fork failed...");
                    break;
                }



            } /* end of if (strncmp(d->d_name, "build", 5) == 0) */
            else 
            {
                pid = fork();

                if (pid > 0)
                {
                    /* 父进程 */
                    wait(NULL);
                }
                else if (pid == 0)
                {
					//memset(str, 0, sizeof(str));
                    /* 子进程 */
                    sprintf(str, "%s%s%s%s", d->d_name, "/", d->d_name, ".pro.user");
                    
                    //sprintf(str, "%s%s%s", d->d_name, "/","*.pro.user");
                #ifdef DEBUG
                    printf("%s\n", str);
                    _exit(-1);
                #endif
                    execl("/usr/bin/rm", "rm", "-rf", str, NULL);
                }
                else if (pid < 0)
                {
                    perror("fork failed...");
                    break;
                }
            } 
        } /* end of if ((d->d_type | DT_DIR) == DT_DIR ) */

    } /* end of while (1) */







    return 0;
err0:
    return -1;
}
